// pages/user/combo/combo.js
import { onRecord } from '../../../utils/integral-record.js'
const config = getApp().globalData.config
const T = getApp().globalData.T
let token = ''
let openId = -1
Page({

  /**
   * 页面的初始数据
   */
  data: {
    comboFtnList:[1,2,3],
    name:['A','B','C','D','E','F','G'],
    _index: -1,
    id: -1, 
    desc: '',
    selectedImg:'/resource/image/radio_select.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      platform: getApp().globalData.platform
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    token = getApp().globalData.token;
    openId = getApp().globalData.openId;
    this.getDetail();
  },

  onQuery() {
    wx.navigateTo({
      url: './query/query',
    })
  },

  getDetail() {
    T.req({
      loadTitle: '加载中...',
      url: config.api + 'service_unit/list',
      method: 'GET',
      data: {
        token: token
      },
      success: (res) => {
        this.setData({
          list: res
        })
      }
    })
  },

  onSelectComb(e) {
    let index = e.currentTarget.dataset.index;
    let id = e.currentTarget.dataset.id;
    let text = e.currentTarget.dataset.text;
    this.setData({
      _index: index,
      id: id,
      desc: '购买' + text + '套餐'
    })
  },

  onAddCombo(orderId) {
    let param = {
      token: token,
      id:orderId
    }
    T.req({
      url: config.api + 'service_residue?' + T.param(param),
      method: 'POST',
      success: (res) => {
        T.toast({
          id: '#toast', text: '购买成功', success: () => {
            wx.navigateBack({
              delta: 1
            })
          }
        });
      }
    })
  },

  onBuy() {
    if (this.data.id == -1) {
      T.toast({ id: '#toast', text: '请选择套餐' });
      return ;
    }
    T.req({
      loadTitle: '请求中...',
      url: config.api + 'pay/wechat/tradepay?token=' + token,
      method: 'POST',
      data: {
        openid: openId,
        goodsId: this.data.id,
        body: this.data.desc,
        goodsType: 0,
        topUpType: -1
      },
      success: (res) => {
        wx.requestPayment({
          timeStamp: res.timeStamp,
          nonceStr: res.nonceStr,
          package: res.package,
          signType: res.signType,
          paySign: res.paySign,
          success: () => {
            this.onAddCombo(res.outTradeNo);
            onRecord(res.outTradeNo);
          }
        })
      }
    })
  }
})